﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using BLL;

namespace BookStore.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/login

        public ActionResult login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult login(UserInfo us)
        {
            if (us.UserName == "" || us.UserPwd == "" || us.UserName == null || us.UserPwd == null)
            {
                return Content("<script>alert('请输入账号和密码！');history.go(-1);;</script>");
            }
            else
            {
                int i = T_BLogin.Log(us);
                int id = T_BLogin.Logid(us);
                HttpCookie uscook = new HttpCookie("User");
                uscook.Expires = DateTime.Now.AddDays(7);
                uscook.Values.Add("UserName", us.UserName);//增加属性
                uscook.Values.Add("UserPwd", us.UserPwd);
                uscook.Values.Add("UserId", id.ToString());
                Response.AppendCookie(uscook);//确定写入cookie中
                if (i == 0)
                {
                    return Content("<script>alert('登入失败！');history.go(-1);;</script>");
                }
                else if (i == 1)
                {
                    return RedirectToAction("Index", "Admin");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }


            }
        }

    }
}
